package iq.number;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import sun.misc.FloatingDecimal;

public class BigDecimalTest {

	/**
	 * @author dingdahai Jan 8, 2010 5:08:45 PM
	 * @param args
	 */
	public static void main(String[] args) {
		
		BigDecimal decimal = new BigDecimal("0.0");
		System.out.println(decimal.add(new BigDecimal("5.23")));//bigdecimal运算后返回的是新的对象
		System.out.println(decimal);
		
		new BigDecimalTest().precision();

		System.out.printf("%1.20f,%1.20f,%1.15f,%1.15f\n", 2.0f - 0.9f,
				2.0f - 1.9f, 0.1, 1.1);
		System.out.println((2.0f - 0.9f) + "," + (2.0f - 1.9f) + ","
				+ (1.1 * 2.1) + "," + (1.1 * 3.1));
		System.out.println(new FloatingDecimal(2.5).toJavaFormatString());

		Float d = 2.5f;
		double c = d * 1.9;

		System.out.printf("%1.20f, %s", c,
				new FloatingDecimal(c).toJavaFormatString());
	}

	public void precision() {
		String num1 = "30";
		String num2 = "15";// 100%
		System.out.println((30 * (100 + new Double("15")) + 999) / 1000 * 10);
		// (charge*( 100+"15" )+999)/1000*10
		BigDecimal bg = new BigDecimal(num1)
				.multiply(new BigDecimal("100").add(new BigDecimal("15")))
				.add(new BigDecimal("999")).divide(new BigDecimal("1000"))
				.multiply(new BigDecimal("10"));
		System.out.println(bg);

		BigDecimal big1 = new BigDecimal("245.205");
		DecimalFormat df = new DecimalFormat("0.00");

		System.out.println(df.format(big1));
		System.out.println(new BigDecimal("200.001").setScale(-1,
				BigDecimal.ROUND_UP));

		System.out.println(1.1 * 100);
	}
}
